################################################################################
# Joshua C. Fjelstul, Ph.D.
# "How the Chamber System at the CJEU Undermines the Consistency of the 
# Court's Application of EU Law"
# Journal of Law and Courts
# replication code for Figure 1
################################################################################

# install ggminimal package (a ggplot theme)
# devtools::install_github("jfjelstul/ggminimal")

# library
library(tidyverse)
library(ggplot2)
library(ggminimal)
library(patchwork)

##################################################
# make plot data
##################################################

# load data
load("replication-materials/judgments.RData")

# collapse by year
plot_data_sums <- judgments %>%
  group_by(year, court) %>%
  filter(!(panel_size %in% c(0, 17, 25, 27))) %>%
  filter(!is.na(panel_size)) %>%
  summarize(
    count_single = mean(panel_size == 1),
    count_small = sum(panel_size == 3 & str_detect(formation, "Chamber")),
    count_medium = sum(panel_size == 5 & str_detect(formation, "Chamber")),
    count_grand = sum(str_detect(formation, "Grand")),
    count_full = sum(str_detect(formation, "Plenary"))
  ) %>%
  ungroup()

# convert from wide to long format
plot_data_sums <- pivot_longer(
  plot_data_sums, names_to = "var", names_prefix = "count_", 
  cols = c(count_single, count_small, count_medium, count_grand, count_full)
)

# collapse by year
plot_data_means <- judgments %>%
  filter(panel_size != 0) %>%
  group_by(year, court) %>%
  summarize(
    count_single = mean(panel_size == 1),
    count_small = mean(panel_size == 3 & str_detect(formation, "Chamber")),
    count_medium = mean(panel_size == 5 & str_detect(formation, "Chamber")),
    count_grand = mean(str_detect(formation, "Grand")),
    count_full = mean(str_detect(formation, "Plenary"))
  ) %>%
  ungroup()

# convert from wide to long format
plot_data_means <- pivot_longer(
  plot_data_means, names_to = "var", names_prefix = "count_", 
  cols = c(count_single, count_small, count_medium, count_grand, count_full)
)

# convert to numeric
plot_data_sums$value <- as.numeric(plot_data_sums$value)
plot_data_means$value <- as.numeric(plot_data_means$value)

# reorder factor
plot_data_sums$var <- factor(plot_data_sums$var, levels = c("single", "small", "medium", "grand", "full"), labels = c("Single judge", "3-judge chamber", "5-judge chamber", "Grand Chamber", "Full Court"))
plot_data_means$var <- factor(plot_data_means$var, levels = c("single", "small", "medium", "grand", "full"), labels = c("Single judge", "3-judge chamber", "5-judge chamber", "Grand Chamber", "Full Court"))

##################################################
# Figure 1, Panel A
##################################################

# make CJ plot (sums)
panel_a <- ggplot(filter(plot_data_sums, court == "Court of Justice")) +
  geom_area(aes(x = year, y = value, group = var, fill = var), alpha = 0.7) +
  annotate(geom = "text", label = "Full", x = 1985, y = 30) + 
  annotate(geom = "text", label = "5", x = 2007, y = 120) + 
  annotate(geom = "text", label = "3", x = 2006, y = 325) + 
  annotate(geom = "text", label = "Grand", x = 2010, y = 20) + 
  scale_x_continuous(breaks = seq(1950, 2020, 10), expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_grey(start = 0.9, end = 0.3, guide = FALSE) +
  theme_minimal() + 
  titles_minimal(title = "Panel A: Court of Justice", y = "Number of judgments")

##################################################
# Figure 1, Panel B
##################################################

# make CJ plot (means)
panel_b <- ggplot(filter(plot_data_means, court == "Court of Justice")) +
  geom_area(aes(x = year, y = value, group = var, fill = var), alpha = 0.7) +
  annotate(geom = "text", label = "Full", x = 1970, y = 0.2) + 
  annotate(geom = "text", label = "5", x = 2010, y = 0.4) + 
  annotate(geom = "text", label = "3", x = 1990, y = 0.9) + 
  annotate(geom = "text", label = "Grand", x = 2010, y = 0.05) + 
  scale_x_continuous(breaks = seq(1950, 2020, 10), expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_grey(start = 0.9, end = 0.3, name = NULL) +
  theme_minimal() + 
  titles_minimal(title = "Panel B: Court of Justice", y = "Proportion of judgments") + 
  theme(
    legend.margin = ggplot2::margin(0, 0, 0, 35)
  )

##################################################
# Figure 1, Panel C
##################################################

# make GC plot (sums)
panel_c <- ggplot(filter(plot_data_sums, court == "General Court")) +
  geom_area(aes(x = year, y = value, group = var, fill = var), alpha = 0.7) +
  annotate(geom = "text", label = "3", x = 2010, y = 120) + 
  annotate(geom = "text", label = "5", x = 1998.25, y = 18) + 
  scale_x_continuous(breaks = seq(1950, 2020, 5), expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_grey(start = 0.9, end = 0.3, guide = FALSE) +
  theme_minimal() + 
  titles_minimal(title = "Panel C: General Court", y = "Number of judgments")

##################################################
# Figure 1, Panel D
##################################################

# make GC plot (means)
panel_d <- ggplot(filter(plot_data_means, court == "General Court")) +
  geom_area(aes(x = year, y = value, group = var, fill = var), alpha = 0.7) +
  annotate(geom = "text", label = "1", x = 2003.5, y = 0.96) + 
  annotate(geom = "text", label = "3", x = 2010, y = 0.55) + 
  annotate(geom = "text", label = "5", x = 1997, y = 0.10) + 
  scale_x_continuous(breaks = seq(1950, 2020, 5), expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_grey(start = 0.9, end = 0.3, name = NULL) +
  theme_minimal() + 
  titles_minimal(title = "Panel D: General Court", y = "Proportion of judgments") + 
  theme(
    legend.margin = ggplot2::margin(0, 0, 0, 35)
  )

##################################################
# Figure 1
##################################################

# combine panels
figure_1 <- panel_a + panel_b + panel_c + panel_d + plot_layout(ncol = 2, nrow = 2, guides = "collect")

# save plot
ggsave(plot = figure_1, filename = "replication-materials/figure-1.pdf", device = "pdf", height = 8, width = 10, scale = 1.25)

################################################################################
# end R script
################################################################################
